<?php
// 启动脚本 (php /jinyunwww/www/web.jinyunweb.com/addons/jy_agentmcp/start_mcpdemo.php)
ini_set('swoole.log_file', '/tmp/swoole_ws_test.log');
ini_set('swoole.debug_mode', 1);

\Swoole\Coroutine\run(function () {
    try {
        $client = new Swoole\Coroutine\Http\Client('api.xiaozhi.me', 443, true);
        $client->set(['timeout' => 5]);
        
        // 尝试升级到 WebSocket
        $ret = $client->upgrade('/mcp/?token=eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjM5NTU5MSwiYWdlbnRJZCI6Njk5NjAxLCJlbmRwb2ludElkIjoiYWdlbnRfNjk5NjAxIiwicHVycG9zZSI6Im1jcC1lbmRwb2ludCIsImlhdCI6MTc1ODU5ODQ3MywiZXhwIjoxNzkwMTU2MDczfQ.AzrYV3VMAGx4WHoQeeFasbLRPQP6GIlV7eetaHVng4egThkjf3OGX8gfvFIpuAIo3KenVV03xV-oQLtqTFRGYA');
        
        if (!$ret) {
            throw new Exception("WebSocket 升级失败: " . $client->errMsg . " (错误码: " . $client->errCode . ")");
        }
        
        echo "WebSocket 连接成功\n";
        
        // 发送测试消息
        $client->push(json_encode(['type' => 'test', 'data' => 'hello']));
        
        // 接收消息
        $frame = $client->recv();
        if ($frame) {
            echo "收到消息: " . $frame->data . "\n";
        }
        
        $client->close();
    } catch (Exception $e) {
        echo "错误: " . $e->getMessage() . "\n";
    }
});
?>